package cz.acies.uniql.metadata;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cz.acies.uniql.dialect.Dialect;

public abstract class ConstraintMetaData implements RelationalModel, Serializable
{
	private static final long serialVersionUID = 1380409620117123878L;
	
	private String name;
	private final List<FieldMetaData> fields = new ArrayList<FieldMetaData>();
	private TableMetaData tblmd;

	private	Dialect dialect = null;
		
	public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
		if (isGenerated(dialect)) {
			return 
			"ALTER TABLE " + 
			tblmd().getQualifiedName(dialect, defaultCatalog, defaultSchema ) + " " +
			"DROP CONSTRAINT " + getName();
		} else {
			return null;
		}
	}
	
	public String sqlCreateString(Dialect dialect, String defaultCatalog, String defaultSchema) {
		if (isGenerated(dialect)) {
			String constraintString = sqlConstraintString(dialect, getName(), defaultCatalog, defaultSchema);
			StringBuffer buf = new StringBuffer("ALTER TABLE ")
			.append( tblmd().getQualifiedName(dialect, defaultCatalog, defaultSchema))
			.append(constraintString);
			return buf.toString();
		} else {
			return null;
		}
	}
	
	// -----  -----------------------------------------------------------------
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Iterator<FieldMetaData> getFieldIterator() {
		return fields.iterator();
	}

	public void addField(FieldMetaData field) {
		if (!fields.contains(field)) fields.add(field);
	}
	/*
	public void addFields(Iterator columnIterator) {
		while (columnIterator.hasNext()) {
			Selectable col = (Selectable) columnIterator.next();
			if (!col.isFormula())
				addColumn((FieldMetaData) col);
		}
	}
	*/
	public boolean containsColumn(FieldMetaData field) {
		return fields.contains(field);
	}

	public int getFieldSpan() {
		return fields.size();
	}

	public FieldMetaData getField(int i) {
		return (FieldMetaData) fields.get(i);
	}

	public Iterator<FieldMetaData> columnIterator() {
		return fields.iterator();
	
	}

	// -----  -----------------------------------------------------------------
	
	public TableMetaData tblmd() {
		return getTableMetaData();
	}
	
	public TableMetaData getTableMetaData() {
		return tblmd;
	}

	public void setTableMetaData(TableMetaData table) {
		this.tblmd = table;
	}

	// -----  -----------------------------------------------------------------
	
	public boolean isGenerated(Dialect dialect) {
		return true;
	}


	
	public List<FieldMetaData> getFields() {
		return fields;
	}

	public abstract String sqlConstraintString(Dialect d, String constraintName, String defaultCatalog, String defaultSchema);
	
	// -----  -----------------------------------------------------------------

	/**
	 * Navraci dialect.
	 * 
	 * @return dialect
	 */
	public Dialect getDialect() {
		return dialect;
	}

	
	/**
	 * Zadava dialect.
	 * 
	 * @param dialect - dialect
	 */
	public void setDialect(Dialect dialect) {
		this.dialect = dialect;
	}

	// -----  -----------------------------------------------------------------
	
	public String toString() {
		return getClass().getName() + '(' + tblmd().getName() + getFields() + ") AS " + name;
	}
	
}
